<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\QH\SchoolSpecStoreRequest;
use App\Models\Qh_SchoolSpec;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class SchoolSpecController extends AdminBaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse|object
     */
    public function index()
    {
        $list = Qh_SchoolSpec::filter(request()->all())->with(['parent'])->orderByDesc('id')->paginate(request('perPage', 10));

        return $this->restful($list);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param SchoolSpecStoreRequest $request
     * @return JsonResponse|object
     */
    public function store(SchoolSpecStoreRequest $request)
    {
        $params = $request->validated();

        if (!empty($params['province'])) $params['province'] = json_encode($params['province'], JSON_UNESCAPED_UNICODE);

        if (!empty($params['parentId'])) {
            $parent = Qh_SchoolSpec::findOrFail($params['parentId']);
            if ($parent->level < 3) $params['level'] = $parent->level + 1;
        }

        $spec = Qh_SchoolSpec::create($params);

        return $this->restful($spec);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param SchoolSpecStoreRequest $request
     * @param int $id
     * @return JsonResponse|object
     */
    public function update(SchoolSpecStoreRequest $request, int $id)
    {
        $params = $request->validated();
        if (!empty($params['province'])) $params['province'] = json_encode($params['province'], JSON_UNESCAPED_UNICODE);

        if (!empty($params['parentId'])) {
            $parent = Qh_SchoolSpec::findOrFail($params['parentId']);
            if ($parent->level < 3) $params['level'] = $parent->level + 1;
        }

        $result = Qh_SchoolSpec::where('id', $id)->update($params);
        return $this->restful($result);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return JsonResponse|object
     */
    public function destroy(int $id)
    {
        $result = Qh_SchoolSpec::where('id', $id)->delete();
        return $this->restful($result);
    }

    public function getSpecOptions() {
        $level = \request('level');
        $parentId = \request('parentId');
        if (empty($level) && $level != 0) {
            return $this->restful([]);
        }
        $list = Qh_SchoolSpec::selectRaw('id, id as value, name as label')
            ->where('parentId', $parentId)
            ->where('level', $level)
            ->orderByRaw('id asc, name asc')->get()->toArray();

        return $this->restful($list);
    }

    public function getSpecOptions2() {
        $list = Qh_SchoolSpec::selectRaw('id, id as value, name as label')
            ->where('level', 1)
            ->orderByRaw('id asc, name asc')->get();

        foreach ($list as $l) {
            $l->children = Qh_SchoolSpec::selectRaw('id, id as value, name as label')
                ->where('parentId', $l->id)
                ->where('level', 2)
                ->orderByRaw('id asc, name asc')->get();
        }

        return $this->restful($list);
    }
}
